﻿using AutoMapper;
using Microsoft.AspNetCore.Mvc;
using Panda.DynamicWebApi;
using Panda.DynamicWebApi.Attributes;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WareHouse.Domain.ModelClass;
using WareHouse.Repositories;

namespace WareHose.Application.WareHouseRomve
{
    /// <summary>
    /// 出库管理
    /// </summary>
    [DynamicWebApi]
    [ApiExplorerSettings(GroupName = "v1")]
    public class OutHouseService : IDynamicWebApi
    {
        private readonly IBaseRepository<StockRemove> sRepository;
        private readonly ISqlSugarClient sugarClient;
        private readonly IMapper mapper;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="sRepository"></param>
        /// <param name="sugarClient"></param>
        /// <param name="mapper"></param>
        public OutHouseService(IBaseRepository<StockRemove> sRepository, ISqlSugarClient sugarClient, IMapper mapper)
        {
            this.mapper = mapper;
            this.sRepository = sRepository;
            this.sugarClient = sugarClient;
        }
        /// <summary>
        /// 新增移库
        /// </summary>
        /// <param name="StockRemove"></param>
        /// <returns></returns>
        [HttpPost]

        public async Task<ResultDto> AddWareHouseRomve(StockRemove StockRemove)
        {
            try
            {

                if (await sRepository.FistAsync(x => x.StockRemoveId == StockRemove.StockRemoveId) == null)
                {
                   
                    if (await sRepository.InsertAsync(StockRemove) != 0)
                    {
                        return new ResultDto { Code = Status.Success, Message = "添加成功" };
                    }
                    else
                    {
                        return new ResultDto { Code = Status.Success, Message = "添加失败" };
                    }

                }
                else
                {
                    return new ResultDto { Code = Status.Success, Message = "此出库记录已存在" };
                }


            }
            catch (Exception e)
            {

                return new ResultDto { Code = Status.Failed, Message = e.Message };
            }
        }
        /// <summary>
        /// 移库条件分页显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]

        public async Task<ResultDto<PagingDto<StockRemove>>> ShowWareHouseRomve(long StockRemoveId, int stockRemoveType, int pageindex = 1, int pagesize = 10)
        {
            try
            {
                //条件从那个表中获取
                var exp = Expressionable.Create<StockRemove>();
                exp.AndIF((StockRemoveId != 0), m => m.StockRemoveId == StockRemoveId);
                exp.And(m => m.IsDel == true);
                exp.AndIF(stockRemoveType != 0, m => m.StockRemoveType == (StockRemoveType)stockRemoveType);
                

                //分页
                var page = await sRepository.PageAsync(pageindex, pagesize, exp.ToExpression());

                //总页数
                var totalPage = (int)Math.Ceiling((double)page.Item1 / pagesize);
                return new ResultDto<PagingDto<StockRemove>>
                {
                    Code = Status.Success,
                    Message = "成功",
                    Data = new PagingDto<StockRemove>
                    {
                        TotalRows = page.Item1,
                        Rows = page.Item2,
                        TotalPage = totalPage,
                    }
                };
            }
            catch (Exception e)
            {
                return new ResultDto<PagingDto<StockRemove>>
                {
                    Code = Status.Failed,
                    Message = e.Message,
                };
                throw;
            }
        }

        /// <summary>
        /// 产品修改
        /// </summary>
        /// <returns></returns>
        [HttpPut]

        public async Task<ResultDto> UpdProduct(StockRemove StockRemove)
        {
            try
            {
                var res = await sRepository.FistAsync(x => x.StockRemoveId != StockRemove.StockRemoveId);
                if (res == null)
                {
                    await sRepository.UpdateAsync(StockRemove);
                    return new ResultDto
                    {
                        Code = Status.Success,
                        Message = "产品信息修改成功！"
                    };
                }
                else
                {
                    return new ResultDto
                    {
                        Code = Status.Success,
                        Message = "产品信息修改失败！"

                    };
                }
            }
            catch (Exception e)
            {
                return new ResultDto
                {
                    Code = Status.Success,
                    Message = e.Message
                };
                throw;
            }
        }
        /// <summary>
        /// 返回查询到的单条产品信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]

        public async Task<ResultDto<StockRemove>> BackProudtc(string id)
        {
            try
            {
                var res = await sRepository.FistAsync(x => x.StockRemoveId.ToString().Contains(id));
                return new ResultDto<StockRemove>
                {
                    Code = Status.Success,
                    Data = res
                };
            }
            catch (Exception e)
            {
                return new ResultDto<StockRemove>
                {
                    Code = Status.Success,
                    Message = e.Message
                };
                throw;
            }
        }
        /// <summary>
        /// 产品删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpDelete]

        public async Task<ResultDto<bool>> DeleteAsync(long id)
        {
            try
            {

                var result = await sRepository.Delete(id);
                if (result > 0)
                {
                    return new ResultDto<bool>
                    {
                        Code = Status.Success,
                        Data = true,
                        Message = "删除成功！"
                    };
                }
                else
                {
                    return new ResultDto<bool>
                    {
                        Code = Status.Failed,
                        Data = false,
                        Message = "删除失败"
                    };
                }
            }
            catch (Exception e)
            {
                return new ResultDto<bool>
                {
                    Code = Status.Failed,
                    Message = e.Message
                };
                throw;
            }
        }
    }
}
